/***************************************************************************
 *   Copyright (C) 2007 by Marcin Swiechowicz   *
 *   marcin.swiechowicz@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "kdcaudiodelaydelegate.h"

#include <QModelIndex>

#include <KIntSpinBox>

KDCAudioDelayDelegate::KDCAudioDelayDelegate( QObject *parent )
	: QItemDelegate( parent ), p_movie( 0 ), p_index( 0 )
{
	
}

void KDCAudioDelayDelegate::setCurrentData( const KDCMovie *movie, int index )
{
	p_index = index;
	p_movie = movie;
}

QWidget* KDCAudioDelayDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	Q_UNUSED( option );
	Q_UNUSED( index );
	KIntSpinBox *editor = new KIntSpinBox( parent );
	editor->setRange( -5000, 5000 );
	connect( editor, SIGNAL( valueChanged( int ) ), this, SIGNAL( delayChanged( int ) ) );
	return editor;
}

void KDCAudioDelayDelegate::setEditorData( QWidget *editor, const QModelIndex& index ) const
{
	//int value = index.model()->data( index, Qt::DisplayRole ).toInt();
	KIntSpinBox *tmp = static_cast<KIntSpinBox*>( editor );
	tmp->setValue( p_movie->audioStreamGroup()->stream( p_index )->delay() );
}

void KDCAudioDelayDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex& index ) const
{
	KIntSpinBox *tmp = static_cast<KIntSpinBox*>( editor );
	tmp->interpretText();
	model->setData( index, tmp->value() );
}

void KDCAudioDelayDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	Q_UNUSED( index );
	editor->setGeometry( option.rect );
}
